672. 灯泡开关 Ⅱ
为保证权益,题目请参考 672. 灯泡开关 Ⅱ(From LeetCode).
解决方案1
Python
python
# 672. 灯泡开关 Ⅱ
# https://leetcode.cn/problems/bulb-switcher-ii/
from typing import List, Tuple
class Solution:
def flipLights(self, n: int, presses: int) -> int:
n = 5 if n > 5 else n
presses = 9 if presses > 9 else presses
d = set()
def op(lights: List[bool], ops: Tuple[int, int, int]):
lights = lights.copy()
if ops[0] == 1:
for i in [2, 4]:
if i < len(lights):
lights[i] = not lights[i]
if ops[1] == 1:
for i in [1, 3]:
if i < len(lights):
lights[i] = not lights[i]
if ops[2] == 1:
for i in [1, 4]:
if i < len(lights):
lights[i] = not lights[i]
return lights
for l1 in range(presses + 1):
for l2 in range(presses + 1 - l1):
for l3 in range(presses + 1 - l1 - l2):
l4 = presses - l1 - l2 - l3
l2 = l2 + l1
l3 = l3 + l1
l2 = l2 % 2
l3 = l3 % 2
l4 = l4 % 2
ops = (l2, l3, l4)
# print(ops)
res = str(op([False] * (n+1), ops))
d.add(res)
# print(d)
return len(d)
if __name__ == "__main__":
so = Solution()
# print(so.flipLights(1, 1))
print(so.flipLights(2, 1))
print(so.flipLights(3, 1))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60